package com.naiterui.ehp.bs.esign.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.bo.esign.RecomSignParamBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.esign.api.ISignService;
import com.naiterui.ehp.bs.esign.api.ISignService.SealPosVO;
import com.naiterui.ehp.bs.esign.api.bjca.vo.SignFileUploadVO;
import com.naiterui.ehp.bs.esign.api.vo.SignAccountVO;
import com.naiterui.ehp.bs.esign.api.vo.SignFileSnVO;
import com.naiterui.ehp.bs.esign.repository.mysql.IEsignRecomEsignDoctorRepository;
import com.naiterui.ehp.bs.esign.repository.mysql.IEsignRecomEsignPharmacistRepository;
import com.naiterui.ehp.bs.esign.service.IDoctorService;
import com.naiterui.ehp.bs.esign.service.IEsignService;
import com.naiterui.ehp.bs.esign.service.IPharmacistService;
import com.naiterui.ehp.bs.esign.service.IRecomService;
import com.naiterui.ehp.bs.esign.utils.Constants;
import com.naiterui.ehp.bs.esign.vo.esign.PreSignResultVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author guoyongxiang
 * @date 2019/12/2 15:54
 * @since 1.0.0
 */
@Slf4j
@Service
public class EsignRecomServiceImpl implements IRecomService {

    @Autowired
    private IEsignService esignService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private IPharmacistService pharmacistService;

    @Autowired
    private ISignService signService;

    @Autowired
    private IEsignRecomEsignDoctorRepository doctorRepository;

    @Autowired
    private IEsignRecomEsignPharmacistRepository pharmacistRepository;

    @Value("${file.base.prefix}")
    private String basePath;

    @Override
    public PreSignResultBO preSignRecipelDr(RecomSignParamBO signVO) throws BusinessException {
        //BJCA在此处上传文件并设置签章位置,坐标和关键字不可同时设置
        SignFileUploadVO signFileUploadVO = this.getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_DR, 525f, 105f, "");
        SignAccountVO signAccountVO = doctorService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        // 药师cms签名、系统自动签名时，入口指定请求来源，未指定表示默认来源app
        if (signVO.getRequestFrom() != null) {
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        PreSignResultVO vo = esignService.preSignPdf(signFileUploadVO, signAccountVO);

        return PreSignResultBO.builder()
                        .uniqueId(vo.getUniqueId())
                        .faceUrl(vo.getUrl())
                        .build();
    }

    @Override
    public PreSignResultBO preSignRecipelPh(RecomSignParamBO signVO) throws BusinessException {

        SignFileUploadVO signFileUploadVO = this.getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_PH, 235f, 50f, "");
        SignAccountVO signAccountVO = pharmacistService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        //药师cms签名时，入口指定请求来源，未指定表示默认来源app
        if(signVO.getRequestFrom() != null){
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        PreSignResultVO vo = esignService.preSignPdf(signFileUploadVO, signAccountVO);

        return PreSignResultBO.builder()
                              .uniqueId(vo.getUniqueId())
                              .faceUrl(vo.getUrl())
                              .build();
    }

    @Override
    public void signDr(RecomSignParamBO signVO) throws BusinessException {
        SealPosVO sealPosVO = signService.getSealPosConfig().getPrPosDr();
        log.info("signDr sealPosVO: {}", JsonMapper.toJson(sealPosVO));
        //e签宝在此处上传文件并设置签章位置
        SignFileUploadVO signFileUploadVO = this
            .getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_DR, sealPosVO.getX(), sealPosVO.getY(), sealPosVO.getKeyword());
        SignAccountVO signAccountVO = doctorService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        signAccountVO.setPin(signVO.getPin());
        // 药师cms签名、系统自动签名时，入口指定请求来源，未指定表示默认来源app
        if (signVO.getRequestFrom() != null) {
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        esignService.sign(signFileUploadVO, signAccountVO);
    }

    @Override
    public void signPh(RecomSignParamBO signVO) throws BusinessException {
        SealPosVO sealPosVO = signService.getSealPosConfig().getPrPosPh();

        SignFileUploadVO signFileUploadVO = this.getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_PH, sealPosVO.getX(), sealPosVO.getY(), sealPosVO.getKeyword());
        SignAccountVO signAccountVO = pharmacistService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        //药师cms签名时，入口指定请求来源，未指定表示默认来源app
        if (signVO.getRequestFrom() != null) {
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        signAccountVO.setPin(signVO.getPin());
        esignService.sign(signFileUploadVO, signAccountVO);
    }

    @Override
    public PreSignResultBO preSignRecipelAlPh(RecomSignParamBO signVO) throws BusinessException {
        SignFileUploadVO signFileUploadVO = this.getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_AL_PH, 400f, 25f, "");
        SignAccountVO signAccountVO = pharmacistService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        // 设置为调配药师
        signAccountVO.setUserType(Constants.USER_TITLE_AL_PH);
        //药师cms签名时，入口指定请求来源，未指定表示默认来源app
        if(signVO.getRequestFrom() != null){
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        PreSignResultVO vo = esignService.preSignPdf(signFileUploadVO, signAccountVO);

        return PreSignResultBO.builder()
                .uniqueId(vo.getUniqueId())
                .faceUrl(vo.getUrl())
                .build();
    }

    @Override
    public void signAlPh(RecomSignParamBO signVO) throws BusinessException {
        SealPosVO sealPosVO = signService.getSealPosConfig().getPrPosAlPh();

        SignFileUploadVO signFileUploadVO = this.getSignFileUploadVO(signVO, SignFileUploadVO.USER_TYPE_AL_PH, sealPosVO.getX(), sealPosVO.getY(), sealPosVO.getKeyword());
        SignAccountVO signAccountVO = pharmacistService.getSignAccount(signVO.getSignUserId(), signVO.getUniqueId());
        // 设置为调配药师
        signAccountVO.setUserType(Constants.USER_TITLE_AL_PH);
        //药师cms签名时，入口指定请求来源，未指定表示默认来源app
        if (signVO.getRequestFrom() != null) {
            signAccountVO.setRequestFrom(signVO.getRequestFrom());
        }
        signAccountVO.setPin(signVO.getPin());
        esignService.sign(signFileUploadVO, signAccountVO);
    }


    /**
     * <pre>
     *     获取签名文件信息
     * </pre>
     *
     * @param signVO       处方信息
     * @param signUserType 操作用户类型
     * @param x            x坐标
     * @param y            y坐标
     *
     * @return
     */
    private SignFileUploadVO getSignFileUploadVO(RecomSignParamBO signVO, String signUserType, Float x, Float y, String keyword) {

        return SignFileUploadVO.builder()
                               .signFileType(SignFileSnVO.FILE_TYPE_RP)
                               .signUserType(signUserType)
                               .signUserId(signVO.getSignUserId())
                               .bizSn(signVO.getBizSn())
                               .patientName(signVO.getPatientName())
                               .patientGender(signVO.getPatientGender())
                               .patientAge(signVO.getPatientAge( ))
                               .pdfPath(signVO.getPdfPath())
                               .keyword(keyword)
                               .unionId(signVO.getUniqueId())
                               .createdAt(signVO.getCreatedAt())
                               .stampX(x)
                               .stampY(y).build();
    }


}
